/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seclust;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 *
 * @author baga
 */
public class InsertDataSet {

    private File filePath;
    private File namaKey;
    private DBOperator dbOp;

    public InsertDataSet(String path) {
        filePath = new File(path);
        dbOp = new DBOperator();
        printPath(filePath);
    }

    public void printPath(File path) {
        File newFile = null;
        if (path.isDirectory()) {
            for (String name : path.list()) {
                newFile = new File(path.getAbsoluteFile() + "/" + name);
                if (newFile.isDirectory()) {
                    printPath(newFile);
                } else {
                    if (newFile.getAbsolutePath().matches("(?).*originalDocs.*")) {
                        
                        String[] splitString = newFile.getAbsolutePath()
                                .split("/originalDocs");
                        String nameKey = splitString[0].substring(splitString[0]
                                .lastIndexOf("/")+1);
                        try {
                            BufferedReader bis =
                                    new BufferedReader(
                                    new FileReader(newFile.getAbsolutePath()));
                           String s;
                           StringBuffer sb = new StringBuffer();
                           while((s = bis.readLine()) != null) {
                               System.out.println(s + " " + nameKey);
                               sb.append(s);
                           }
                           dbOp.insTableFull("BODY", newFile.getName(), new String(sb), nameKey);
                           bis.close();
                        } catch(IOException ie) {
                            ie.printStackTrace();
                        }
                    }
                }
            }

        } else {
            if (path.getAbsolutePath().matches("(?i).*originalDocs.*")) {
                System.out.println(path.getAbsolutePath());
            }
        }
    }

    public static void main(String[] args) {
        InsertDataSet ids = new InsertDataSet("data_set");
    }
}
